package com.elong.qadev.action;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.elong.qadev.entity.CaseNode;
import com.elong.qadev.service.CaseServiceI;
import com.elong.qadev.service.HttpCaseServiceI;
import com.elong.qadev.service.WebuiCaseServiceI;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @ClassName: CaseNodeAction
 * @Description: TODO(前台caseJson返回值获取相关)
 * @author kangchen
 * @date 2015年9月8日
 *
 */
// @ParentPackage("json-default")
@ParentPackage("all")
public class CaseNodeAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger logger = Logger.getLogger(CaseNodeAction.class);
	@Autowired
	private CaseServiceI caseService;
	@Autowired
	private HttpCaseServiceI httpCaseService;
	@Autowired
	private WebuiCaseServiceI webuiCaseService;
	private String caseNodeJson;
	private String caseId;
	private String caseWebuiJson;

	public String getCaseWebuiJson() {
		return caseWebuiJson;
	}

	public void setCaseWebuiJson(String caseWebuiJson) {
		this.caseWebuiJson = caseWebuiJson;
	}

	public String getCaseId() {
		return caseId;
	}

	public void setCaseId(String caseId) {
		this.caseId = caseId;
	}

	public String getHttpCaseJson() {
		return httpCaseJson;
	}

	public void setHttpCaseJson(String httpCaseJson) {
		this.httpCaseJson = httpCaseJson;
	}

	public String getRunBeforesJson() {
		return runBeforesJson;
	}

	public void setRunBeforesJson(String runBeforesJson) {
		this.runBeforesJson = runBeforesJson;
	}

	public String getHeadersJson() {
		return headersJson;
	}

	public void setHeadersJson(String headersJson) {
		this.headersJson = headersJson;
	}

	public String getUrlParamsJson() {
		return urlParamsJson;
	}

	public void setUrlParamsJson(String urlParamsJson) {
		this.urlParamsJson = urlParamsJson;
	}

	public String getEntitiesJson() {
		return entitiesJson;
	}

	public void setEntitiesJson(String entitiesJson) {
		this.entitiesJson = entitiesJson;
	}

	public String getRunAftersJson() {
		return runAftersJson;
	}

	public void setRunAftersJson(String runAftersJson) {
		this.runAftersJson = runAftersJson;
	}

	public String getCaseNodeJson() {
		return caseNodeJson;
	}

	public void setCaseNodeJson(String caseNodeJson) {
		this.caseNodeJson = caseNodeJson;
	}

	/**
	 * 
	 * @Title: getInterfaceCase @Description:
	 * TODO(初始化ztree的json值) @param @return 参数 @return String 返回类型 @throws
	 */
	@Action(value = "getHttpTreeNode", results = { @Result(name = "success", type = "json") })
	public String getHttpCaseNode() {
		logger.info("执行获取json-ztree内容action");
		try {
			logger.info("初始化json字符串list");
			ArrayList<CaseNode> plist = new ArrayList<CaseNode>();
			logger.info("获取json对象内容");
			plist = caseService.getCases();
			this.caseNodeJson = JSON.toJSONString(plist, true);
			logger.info("拼接完毕，返回：" + this.caseNodeJson);
		} catch (Exception e) {
			// TODO Auto-generated catch block 50.
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 
	 * @Title: getWebuiCaseNode @Description: TODO(返回webui的node值) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@Action(value = "getWebuiTreeNode", results = { @Result(name = "success", type = "json") })
	public String getWebuiCaseNode() {
		logger.info("执行获取webui-ztree内容action");
		try {
			logger.info("初始化webui-json字符串list");
			ArrayList<CaseNode> plist = new ArrayList<CaseNode>();
			logger.info("获取webui-json对象内容");
			plist = caseService.getWebuiCaseNodes();
			this.caseWebuiJson = JSON.toJSONString(plist, true);
			logger.info("拼接完毕，返回：" + this.caseWebuiJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String getNameValuePairsJson() {
		return nameValuePairsJson;
	}

	public void setNameValuePairsJson(String nameValuePairsJson) {
		this.nameValuePairsJson = nameValuePairsJson;
	}

	private String httpCaseJson;
	private String runBeforesJson;
	private String headersJson;
	private String urlParamsJson;
	private String entitiesJson;
	private String nameValuePairsJson;
	private String runAftersJson;
	private String initBeforeJson;
	private String initAfterJson;

	private String webuiCasesJson;

	public String getWebuiCasesJson() {
		return webuiCasesJson;
	}

	public void setWebuiCasesJson(String webuiCasesJson) {
		this.webuiCasesJson = webuiCasesJson;
	}

	/**
	 * 
	 * @Title: getInitBefore @Description: TODO(初始化前置条件的select值) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@Action(value = "getInitBefore", results = { @Result(name = "success", type = "json") })
	public String getInitBefore() {
		logger.info("执行获取initBefore内容action");
		try {
			logger.info("获取json对象内容");
			this.initBeforeJson = JSON.toJSONString(httpCaseService.getBeforeClassInfo(), true);
			logger.info("拼接完毕，返回：" + this.initBeforeJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 
	 * @Title: getInitAfter @Description: TODO(初始化后置条件的select值) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@Action(value = "getInitAfter", results = { @Result(name = "success", type = "json") })
	public String getInitAfter() {
		logger.info("执行获取initAfter内容action");
		try {
			logger.info("获取json对象内容");
			this.initAfterJson = JSON.toJSONString(httpCaseService.getAfterClassInfo(), true);
			logger.info("拼接完毕，返回：" + this.initAfterJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String getInitBeforeJson() {
		return initBeforeJson;
	}

	public void setInitBeforeJson(String initBeforeJson) {
		this.initBeforeJson = initBeforeJson;
	}

	public String getInitAfterJson() {
		return initAfterJson;
	}

	public void setInitAfterJson(String initAfterJson) {
		this.initAfterJson = initAfterJson;
	}

	/**
	 * 
	 * @Title: getHttpCase @Description: TODO(返回http用例基础数据) @param @return
	 * 参数 @return String 返回类型 @throws
	 */
	@Action(value = "getCaseJson", results = { @Result(name = "success", type = "json") })
	public String getHttpCase() {
		if (caseId == null) {
			return SUCCESS;
		}
		logger.info("获取caseId为：" + caseId + "的用例数据");
		logger.info("执行获取json-Case内容action");
		try {
			logger.info("开始获取json字符串");
			// httpCase
			logger.info("获取json对象内容");
			this.httpCaseJson = JSON.toJSONString(httpCaseService.getHttpCase(Integer.parseInt(caseId)), true);
			this.runBeforesJson = JSON.toJSONString(httpCaseService.getRunBefores(Integer.parseInt(caseId)), true);
			this.urlParamsJson = JSON.toJSONString(httpCaseService.getUrlParams(Integer.parseInt(caseId)), true);
			this.headersJson = JSON.toJSONString(httpCaseService.getHeaders(Integer.parseInt(caseId)), true);
			this.entitiesJson = JSON.toJSONString(httpCaseService.getEntities(Integer.parseInt(caseId)), true);
			this.nameValuePairsJson = JSON.toJSONString(httpCaseService.getNameValuePairs(Integer.parseInt(caseId)),
					true);
			this.runAftersJson = JSON.toJSONString(httpCaseService.getRunAfters(Integer.parseInt(caseId)), true);
			logger.info("httpCaseJson拼接完毕，返回：" + this.httpCaseJson);
			logger.info("runBeforesJson拼接完毕，返回：" + this.runBeforesJson);
			logger.info("urlParamsJson拼接完毕，返回：" + this.urlParamsJson);
			logger.info("headersJson拼接完毕，返回：" + this.headersJson);
			logger.info("entitiesJson拼接完毕，返回：" + this.entitiesJson);
			logger.info("nameValuePairsJson拼接完毕，返回：" + this.nameValuePairsJson);
			logger.info("runAftersJson拼接完毕，返回：" + this.runAftersJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Action(value = "getWebuiCaseJson", results = { @Result(name = "success", type = "json") })
	public String getWebuiCaseJson() {
		logger.info("获取caseId为：" + caseId + "的用例数据");
		logger.info("执行获取webui-Case内容action");
		try {
			logger.info("开始获取webui-json字符串");
			logger.info("获取json对象内容");
			this.webuiCasesJson = JSON.toJSONString(webuiCaseService.getWebuiCase(Integer.parseInt(caseId)), true);
			logger.info("webuiCaseJson拼接完毕，返回：" + this.webuiCasesJson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}